<template>
 <div >

      
      <div style="width: 450px;height: 350px;  border: solid 2px;border-radius: 20px ;" class="flex-center shadow-box">
        <el-text class="mx-1" size="large" tag="b">登录</el-text>
        <br>
        
        <el-form :model="form" label-width="auto" style="max-width: 400px " >

          <el-form-item label="账户" label-position="left">
            <el-input v-model="form.name" placeholder="用户名或邮箱" />
          </el-form-item>
          

          <el-form-item label="密码" label-position="left">
            <el-input v-model="form.password" type="password" />
          </el-form-item>
          <el-button class="mx-1" type="text" @click=goToRegister>注册新账户</el-button>
          <br>
          <el-button type="primary" @click="onSubmit">登录</el-button>

        </el-form>
      </div>
    </div>
</template>

<script lang="ts" setup>
import { reactive } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios';
import { ref } from 'vue'
import { ElMessage } from 'element-plus';
// do not use same name with ref

const res = ref('')

const form = reactive({
  name: '',
  password: '',
})

const router = useRouter()
const goToRegister = () => {
  router.push('/register')
}

const onSubmit = () => {

var data = JSON.stringify({
   "userName": form.name,
   "password": form.password
});

var config = {
   method: 'post',
   url: 'http://localhost:8080/users/login',
   headers: { 
      'Content-Type': 'application/json'
   },
   data : data
};

axios(config)
.then(function (response) {
  res= response.data;
   console.log(JSON.stringify(response.data));

})
.catch(function (error) {
   console.log(error);
});

if(res.success = 'success') {
  // alert('登录成功')
  ElMessage({
    type:'success',
    message:'登录成功',
  })
  router.push('/homePage')
} else {  
  alert('登录失败，请检查用户名或密码')
}

  console.log('submit!')
}


</script>

<style scoped>

.flex-center {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100px;
  flex-direction: column;
}

.shadow-box {
  background-color: rgb(238, 240, 249);
  width: 300px;
  height: 200px;
  border-radius: 10px;
  border: 2px solid #4285f4;
  padding: 20px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

</style>